﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using AutoMapper;
using Kalman;
using FreeSql;
using Microsoft.AspNetCore.Authorization;
using System.Text;
using Microsoft.Extensions.Configuration;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Formats.Jpeg;
using Wechat.TenpayV3;
using RestSharp;
using System.IO;
using Wechat.MP;
using Newtonsoft.Json.Linq;

namespace QuDao.Api.Controllers
{
    /// <summary>
    /// 测试API
    /// </summary>
    [AllowAnonymous]
    [Obsolete]
    public class TestController : ApiControllerBase
    {
    	readonly IMapper _mapper = null;
        readonly IFreeSql db;

        public TestController(IMapper mapper, IFreeSql freeSql)
        {
            _mapper = mapper;
            db = freeSql;
        }

        [HttpGet]
        public long GetNewID(DateTime pubDate)
        {
            //var id = IDGenerator.Instance.GetIDByH4096() * 1000 + RandomUtil.GetInteger(0, 999);
            ////var id = long.Parse($"{DateTime.Now:yyMMddHHmmss}{RandomUtil.GetInteger(0, 999)}");
            ////var id = WxUserBO.GetNewID();
            //return id;

            var beginTime = DateTime.Parse("2012-01-01");
            var endTime = DateTime.Now;
            if (pubDate > DateTimeConst.MIN) endTime = DateTime.Parse($"{pubDate:yyyy-MM-dd} {endTime: HH:mm:ss}");

            //var id = TimeStampUtil.GetTimeStamp(beginTime, endTime, Kalman.Enums.TimePrecision.Second) ;
            var id = TimeStampUtil.GetTimeStamp(beginTime, endTime, Kalman.Enums.TimePrecision.Second) * 1000 + RandomUtil.GetInteger(0, 999);
            //id = long.Parse($"{id}{DateTime.Now:fff}");

            return id;
        }

        [HttpGet]
        public string GetRandom()
        {
            var sb = new StringBuilder();
            for (int i = 0; i < 100; i++)
            {
                sb.AppendLine(RandomUtil.GetInteger(0, 999).ToString());
            }
            return sb.ToString();
        }

        [HttpGet]
        public string GetCS()
        {
            //var cs = config.GetConnectionString("cn_main");
            //return cs;
            var cs = AppConfig.Instance.GetConnectionString("cn_main");
            return cs;
        }

        [HttpGet]
        public List<Area> GetList()
        {
            //var list = db.Select<Area>().Where(c => c.AreaLevel == 1).Page(1, 20).ToList();
            //return list;
            return AreaBO.Instance.ProvinceList;
        }

        [HttpGet]
        public string DownImage(string url)
        {
            var client = new RestClient();
            var request = new RestRequest(url, DataFormat.None);
            request.AddHeader("Accept", "*.*");
            request.AddHeader("Content-Type", "image/jpeg");
            //var data = client.DownloadData(request);
            var response = client.Get(request);

            var data = response.RawBytes;
           

            //var extName = Path.GetExtension(url);
            var fileName = "111.jpg";// Path.GetFileName(url);



            using (var fs = new FileStream($"c:\\{fileName}", FileMode.Create)) 
            {
                fs.Write(data, 0, data.Length);
            }


            var s = Encoding.Default.GetString(data);
            return s;
        }

        //[HttpGet]
        //public string ReplaceLogo()
        //{
        //    var url = WxaCodeBO.Instance.GetQrCode(1004, 0, "pages/tenantDetail/tenantDetail", "T", 1004, true);
        //    return url;
        //}

        //[HttpPost]
        //public string GetAccessToken()
        //{
        //    var result = WxMPClient.Instance.GetAccessToken();
        //    return result;
        //}

        //[HttpGet]
        //public JObject GetUserList(string next_openid = "")
        //{
        //    var jo = WxMPClient.Instance.GetUserList(next_openid);
        //    return jo;
        //}

        [HttpGet]
        public string SyncOrderData(int tid, DateTime beginDate, DateTime endDate)
        {
            GoodsSalesBO.Instance.SyncOrderData(tid, beginDate, endDate);
            return "OK";
        }
    }
}
